﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Acme.BookStore2.Permissions;
using Microsoft.AspNetCore.Authorization;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Services;
using Volo.Abp.ObjectExtending;

namespace Acme.BookStore2
{
    [Authorize(BookStore2Permissions.QuestionAndAnswerPost.Default)]
    public class QuestionAndAnswerPostAppService : ApplicationService, IQuestionAndAnswerPostAppService
    {
        private readonly IQuestionAndAnswerPostRepository _questionAndAnswerPostRepository;

        public QuestionAndAnswerPostAppService(IQuestionAndAnswerPostRepository questionAndAnswerPostRepository)
        {
            _questionAndAnswerPostRepository = questionAndAnswerPostRepository;
        }

        public async Task<QuestionAndAnswerPostDto> GetAsync(Guid id)
        {
            return ObjectMapper.Map<QuestionAndAnswerPost, QuestionAndAnswerPostDto>(await _questionAndAnswerPostRepository.GetAsync(id, false));
        }

        public async Task<PagedResultDto<QuestionAndAnswerPostDto>> GetPagedListAsync(GetQuestionAndAnswerPostsInput input)
        {
            var count = await _questionAndAnswerPostRepository.GetCountAsync(input.Title);
            var list = await _questionAndAnswerPostRepository.GetPagedListAsync(input.Sorting, input.MaxResultCount, input.SkipCount, input.Title);
            return new PagedResultDto<QuestionAndAnswerPostDto>(count, ObjectMapper.Map<List<QuestionAndAnswerPost>, List<QuestionAndAnswerPostDto>>(list));
        }

        [Authorize(BookStore2Permissions.QuestionAndAnswerPost.Create)]
        public async Task<QuestionAndAnswerPostDto> CreateAsync(QuestionAndAnswerPostCreateDto input)
        {
            var questionAndAnswerPost = new QuestionAndAnswerPost(GuidGenerator.Create(), input.Title)
            {
                Description = input.Description,
                Content = input.Content,
                Tags = input.Tags
            };
            input.MapExtraPropertiesTo(questionAndAnswerPost);

            await _questionAndAnswerPostRepository.InsertAsync(questionAndAnswerPost);
            
            return ObjectMapper.Map<QuestionAndAnswerPost, QuestionAndAnswerPostDto>(questionAndAnswerPost);
        }

        [Authorize(BookStore2Permissions.QuestionAndAnswerPost.Update)]
        public async Task<QuestionAndAnswerPostDto> UpdateAsync(Guid id, QuestionAndAnswerPostUpdateDto input)
        {
            var questionAndAnswerPost = await _questionAndAnswerPostRepository.GetAsync(id);
            questionAndAnswerPost.ConcurrencyStamp = input.ConcurrencyStamp;
            questionAndAnswerPost.SetTitle(input.Title);
            questionAndAnswerPost.Description = input.Description;
            questionAndAnswerPost.Content = input.Content;
            questionAndAnswerPost.Tags = input.Tags;
            input.MapExtraPropertiesTo(questionAndAnswerPost);

            await _questionAndAnswerPostRepository.UpdateAsync(questionAndAnswerPost);

            return ObjectMapper.Map<QuestionAndAnswerPost, QuestionAndAnswerPostDto>(questionAndAnswerPost);
        }

        [Authorize(BookStore2Permissions.QuestionAndAnswerPost.Delete)]
        public async Task DeleteAsync(Guid id)
        {
            var questionAndAnswerPost = await _questionAndAnswerPostRepository.FindAsync(id);
            if (questionAndAnswerPost == null)
            {
                return;
            }
            await _questionAndAnswerPostRepository.DeleteAsync(questionAndAnswerPost);
        }
    }
}